/**
 * 
 */
package com.tunan.lsj.validator;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.tunan.common.Result;
import com.tunan.common.StringUtils;

/**
 * 校验是否有重复的企事业单位
 * 
 * @author czhcc
 *
 */
@Component(value="corpCk")
public class CorpCheckHandler implements IServerCheckHandler
{
	@Autowired
	JdbcTemplate jt;
	/**
	 * 
	 */
	private static final long serialVersionUID = -964547837614055889L;
	
	private static final String sql = "select count(qybh) from t_gg_dwxx where xzq=? and dwmc=?";

	/* (non-Javadoc)
	 * @see org.autumn.form.IServerCheckHandler#verify(java.lang.String, org.IGenericModel, java.lang.String)
	 */
	public Result verify(String propertyName, Map model, String userId)
	{
		if( model.get("xzq") != null 
				&& model.get("dwmc") != null  )
		{
			String jz = model.get("xzq").toString();
			String dwmc = model.get("dwmc").toString();
			if( StringUtils.hasText(jz) && StringUtils.hasText(dwmc))
			{

				Long count = jt.queryForObject(sql, new Object[] {jz,dwmc},new RowMapper<Long>(){

					@Override
					public Long mapRow(ResultSet arg0, int arg1)
							throws SQLException {
						// TODO Auto-generated method stub
						return (Long)arg0.getObject(1);
					}
				});
				if( count > 0 )
				{
					return new Result(false,"录入的企事业单位已存在");
				}
			}
		}
		
		return new Result();
	}

}
